<template>
	<el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large">
		<el-form-item prop="user">
			<el-input 
				v-model="form.user" 
				prefix-icon="User" 
				clearable 
				:placeholder="$t('login.userPlaceholder')"
				class="login-input">
			</el-input>
		</el-form-item>
		<el-form-item prop="password">
			<el-input 
				v-model="form.password" 
				prefix-icon="Lock" 
				clearable 
				show-password 
				:placeholder="$t('login.PWPlaceholder')"
				class="login-input">
			</el-input>
		</el-form-item>
		<el-form-item>
			<el-button 
				type="primary" 
				class="login-button" 
				:loading="islogin" 
				@click="login">
				{{ $t('login.signIn') }}
			</el-button>
		</el-form-item>
	</el-form>
</template>

<script>
	export default {
		data() {
			return {
				userType: 'admin',
				form: {
					user: "",
					password: "",
					autologin: false,
					captcha: '',
					key: ''
				},
				captchaSrc: '',
				rules: {
					user: [
						{required: true, message: this.$t('login.userError'), trigger: 'blur'}
					],
					password: [
						{required: true, message: this.$t('login.PWError'), trigger: 'blur'}
					],
					captcha: [
						{required: true, message: '请输入验证码', trigger: 'blur'}
					]
				},
				islogin: false,
			}
		},
		mounted() {
			//this.changeCaptcha()

			document.onkeydown = (event) => {
                var e = event || window.event;
                if (e && e.keyCode == 13) {
                    this.login();
                }
            }
		},
		methods: {
			// 更换验证码
			async changeCaptcha() {
				let res = await this.$API.auth.captcha.get()
				this.captchaSrc = res.data.captchaImage
				this.form.key = res.data.captchaId
			},
			async login() {

				var validate = await this.$refs.loginForm.validate().catch(()=>{})
				if(!validate){ return false }

				this.islogin = true
				var data = {
					username: this.form.user,
					password: this.form.password,
					captcha: this.form.captcha,
					key: this.form.key
				}
				//获取token
				var user = await this.$API.auth.token.post(data)
				if (user.code == 0) {
					this.$TOOL.data.set("TOKEN", user.data.token)
					this.$TOOL.data.set("USER_INFO", user.data.userInfo)

					// 获取菜单
					if (user.data.menu.length ==0) {
						this.islogin = false
						this.$alert("当前用户无任何菜单权限，请联系系统管理员", "无权限访问", {
							type: 'error',
							center: true
						})
						return false
					}
					this.$TOOL.data.set("MENU", user.data.menu)
					//this.$TOOL.data.set("PERMISSIONS", menu.data.permissions)

				}else{
					this.islogin = false
					this.$message.warning(user.msg)
					this.changeCaptcha()
					return false
				}


				this.$router.replace({
					path: '/'
				})
				this.$message.success("登录成功")
				this.islogin = false
			},
		}
	}
</script>

<style scoped>
.login-input {
	transition: all 0.3s ease;
}

.login-input:deep(.el-input__wrapper) {
	padding: 0 16px;
	height: 48px;
	border-radius: 8px;
	border: 1px solid #e0e6ed;
	background: #fff;
	box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
	transition: all 0.3s ease;
}

.login-input:deep(.el-input__wrapper:hover) {
	border-color: #0170FE;
	box-shadow: 0 4px 12px rgba(1, 112, 254, 0.1);
}

.login-input:deep(.el-input__wrapper.is-focus) {
	border-color: #0170FE;
	box-shadow: 0 0 0 3px rgba(1, 112, 254, 0.1);
}

.login-input:deep(.el-input__inner) {
	height: 48px;
	font-size: 14px;
	color: #333;
}

.login-input:deep(.el-input__prefix) {
	color: #999;
}

.login-button {
	width: 100%;
	height: 48px;
	border-radius: 8px;
	font-size: 16px;
	font-weight: 500;
	background: linear-gradient(135deg, #0170FE 0%, #0052cc 100%);
	border: none;
	transition: all 0.3s ease;
	box-shadow: 0 4px 12px rgba(1, 112, 254, 0.3);
}

.login-button:hover {
	background: linear-gradient(135deg, #0052cc 0%, #0041a3 100%);
	transform: translateY(-1px);
	box-shadow: 0 6px 16px rgba(1, 112, 254, 0.4);
}

.login-button:active {
	transform: translateY(0);
	box-shadow: 0 2px 8px rgba(1, 112, 254, 0.3);
}

.login-button.is-loading {
	background: linear-gradient(135deg, #0170FE 0%, #0052cc 100%);
}

:deep(.el-form-item) {
	margin-bottom: 24px;
}

:deep(.el-form-item__content) {
	line-height: 48px;
}
</style>
